/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gracefulmm.dealImage;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

/**
 *
 * @author wenbo
 */
public class ImageTools {

    /**
     * 获取文件的类型
     *
     * @param fileName
     * @return
     */
    public static String getImageType(String fileName) {
        if (StringUtils.isNotBlank(fileName)) {
            int i = fileName.lastIndexOf(".");
            if (i == -1) {
                return null;
            }
            return StringUtils.substring(fileName, i + 1, fileName.length());
        }
        return null;
    }

    /**
     * 检测图片是否要处理
     *
     * @param name
     * @return
     */
    public static boolean isDeal(String name) {
        if (StringUtils.isNotBlank(name)) {
            if (StringUtils.contains(name, CommonConstants.ALREADY_DEAL)) {
                return true;
            }else{
                return false;
            }
        }
        return true;
    }

    /**
     * 处理图片
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String saveNewImageAsJpg(String path,
            int width, int height) {
        if (StringUtils.isBlank(path) || width < 0 || height < 0) {
            //logger.error("parmeter is error");
            return null;
        }
        try {
            IMOperation op = new IMOperation();
            op.addImage(path);
            op.resize(width, height, ">").depth(16).quality(99.0).filter("cubic").gamma(1.0).sharpen(0.5).despeckle();
            op.addImage(path + "_" + width + "x" + height + "_" + CommonConstants.ALREADY_DEAL + ".png");
            ConvertCmd convert = new ConvertCmd(true);
            convert.run(op);
        } catch (Exception e) {
            //logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 给图片添加水印
     *
     * @param path
     * @param width
     * @param height
     * @return
     */
    public static String addWatermark(String watermarkPath,String inputPath,String outputPath) {
        if (StringUtils.isBlank(inputPath)
                || StringUtils.isBlank(outputPath)) {
            //logger.error("parmeter is error");
            return null;
        }
        try {
//            convert dragon.gif   -background Khaki  label:'Faerie Dragon' \
//                   -gravity Center -append    anno_label.jpg
            IMOperation op = new IMOperation();
            op.addImage(inputPath);
            op.background("Khaki").label("Faerie Dragon").gravity("Center").append();
            op.addImage(outputPath);
            System.out.println(op.toString());
            ConvertCmd convert = new ConvertCmd(true);
            convert.run(op);
        } catch (Exception e) {
            //logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取淘宝操作对象
     * @return 
     */
    public static TaobaoClient getTaobaoClient(){
        //如果今天没更新过淘宝sessionkey那么更新
        if(TaobaoDealUtil.taobaoClient == null){
            TaobaoClient taobaoClient = new DefaultTaobaoClient(CommonConstants.TAOBAO_SERVER_URL,CommonConstants.TAOBAO_APPLICTION_KEY,
                CommonConstants.TAOBAO_APPLICTION_SECRET);
            TaobaoDealUtil.taobaoClient = taobaoClient;
        }
        return TaobaoDealUtil.taobaoClient;
    }

    public static void main(String[] args) {
        String inputPath = "/Users/wenbo/Desktop/pic/test/2.png";
        String outputPath = "/Users/wenbo/Desktop/pic/test/4.jpg";
        saveNewImageAsJpg(inputPath,100,100);        
    }
}
